//
//  ExploreViewController.m
//  ShangHaiTuJI
//
//  Created by xiaoxiangtuji on 2017/7/19.
//  Copyright © 2017年 xiaoxiangtuji. All rights reserved.
//

#import "ExploreViewController.h"
#import "YYInfiniteLoopView.h"
#import "ListenStoryCell.h"
#import "ListenStoryHeaderCell.h"
#import "IntelligentCell.h"
#import "LearnSkillMiddleCell.h"
#import "LearnSkillBottomCell.h"
#import "PGIndexBannerSubiew.h"
#import "NewPagedFlowView.h"
#import "CelebrityBiographyDetailController.h"
#import "CelebrityListViewController.h"
#import "OpusListModel.h"
#import "BigAndSmallPersonViewController.h"
#import "IntorduceAndCatalogViewController.h"
#import <UShareUI/UShareUI.h>
typedef NS_ENUM(NSUInteger,UIButtonResponderType){
    UIButtonIntelligent = 1000,
    UIButtonLearnAbilitys,
    UIButtonListenStory ,
};

@interface ExploreViewController ()<YYInfiniteLoopViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,NewPagedFlowViewDelegate,NewPagedFlowViewDataSource>{
    NSInteger _showCount;
    NSInteger _currentPage;
}

@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)UICollectionViewFlowLayout *flowLayout;
@property(nonatomic,strong)UIView *headerView;
@property(nonatomic,assign)UIButtonResponderType buttonType;
@property(nonatomic,strong)NSMutableArray *imageArra;
@property(nonatomic,strong)NSMutableArray *LearSkillBottomArray;
@property(nonatomic,strong)NSMutableArray *ListenStoryBottomArray;
@end

@implementation ExploreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"秘探";
//    [self getPictureAndId:@"3"];
//     [self getHomePageAllData:@"3"];
//    [self getHomePageAllData:@"3"];
    // Do any additional setup after loading the view.
//    [self.collectionView.mj_header beginRefreshing];
}
-(void)viewWillAppear:(BOOL)animated{
     [super viewWillAppear:animated];
    [self createHeaderView];
    _buttonType = UIButtonIntelligent;
    [self createCollectionUI:_headerView];
     [self.collectionView.mj_header beginRefreshing];
    self.tabBarController.tabBar.hidden = NO;
}

#pragma mark -图片轮播
- (void)loopViewDelegate:(UIView *) view AndAddToFatherView:(UIView *)faterView{
    NSArray *array = @[@"1.jpg",@"2.jpg",@"未标题-2.jpg",@"4.jpg",];
    // 类方法
    YYInfiniteLoopView *loopView = [YYInfiniteLoopView
                                    infiniteLoopViewWithImageUrls:array
                                    titles:nil
                                    didSelectedImage:^(NSInteger index) {
                                        NSLog(@"%zd",index);
                                    }];
    // 设置代理
    loopView.delegate = self;
    loopView.timeInterval = 1;
    loopView.currentPageIndicatorColor = [UIColor yellowColor];
    loopView.pagePosition = InfiniteLoopViewPagePositionCenter;
    loopView.frame = CGRectMake(0, 0, WIDTH, scaleTopAndBottom(175));
    [faterView addSubview:loopView];
}
#pragma mark - YYInfiniteLoopViewDelegate
- (void)infiniteLoopView:(YYInfiniteLoopView *)infiniteLoopView
        didSelectedImage:(NSInteger)selectedImageIndex {
    [self didSelectedImageWithIndex:selectedImageIndex];
}

- (void)didSelectedImageWithIndex:(NSInteger)index {
    NSLog(@"点击了%ld",index);
}

#pragma mark -创建头部视图和听故事的首页页面


-(void)createHeaderView{
    self.headerView = [[UIView alloc] init];
    _headerView.backgroundColor = ZColorRGB(247, 216, 73);
    [self.view addSubview:_headerView
     ];
    [_headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.top.equalTo(self.view.mas_top).offset(scaleLeftAndRight(65));
        make.height.mas_equalTo(scaleTopAndBottom(50));
    }];
    UIButton *intelligentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    intelligentBtn.tag = 1000;
    [intelligentBtn addTarget:self action:@selector(ButtonDialAndResponder:) forControlEvents:UIControlEventTouchUpInside];
    intelligentBtn.backgroundColor = [UIColor purpleColor];
    [_headerView addSubview:intelligentBtn];
    UIButton *learnBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    learnBtn.tag = 1001;
    [learnBtn addTarget:self action:@selector(ButtonDialAndResponder:) forControlEvents:UIControlEventTouchUpInside];
    learnBtn.backgroundColor = [UIColor purpleColor];
    [_headerView addSubview:learnBtn];
    UIButton *listenBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    listenBtn.tag = 1002;
    listenBtn.backgroundColor = [UIColor purpleColor];
    [listenBtn addTarget:self action:@selector(ButtonDialAndResponder:) forControlEvents:UIControlEventTouchUpInside];
    [_headerView addSubview:listenBtn];
    [learnBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(_headerView.mas_centerX);
        make.height.mas_equalTo(scaleTopAndBottom(50));
        make.width.mas_equalTo(intelligentBtn.mas_width);
        make.width.mas_equalTo(listenBtn.mas_width);
        make.top.equalTo(_headerView.mas_top);
    }];
    [intelligentBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_headerView.mas_left).offset(scaleLeftAndRight(10));
        make.top.equalTo(_headerView.mas_top);
        make.bottom.equalTo(_headerView.mas_bottom);
        make.right.equalTo(learnBtn.mas_left).offset(scaleLeftAndRight(-80));
    }];
    [listenBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_headerView.mas_right).offset(scaleLeftAndRight(-10));
        make.top.equalTo(_headerView.mas_top);
        make.bottom.equalTo(_headerView.mas_bottom);
        make.left.equalTo(learnBtn.mas_right).offset(scaleLeftAndRight(80));
    }];
}

#pragma mark -创建collectionView
-(void)createCollectionUI:(UIView *)view{
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(view.mas_bottom);
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.bottom.equalTo(self.view.mas_bottom).offset(scaleTopAndBottom(-49));
    }];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Collectionfooter"];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    switch (_buttonType) {
        case UIButtonIntelligent:
        {
            [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"intelligentHeader"];
            UINib *nib = [UINib nibWithNibName:@"IntelligentCell" bundle:nil];
            [self.collectionView registerNib:nib forCellWithReuseIdentifier:@"IntelligentCell"];
            UINib *nib1 = [UINib nibWithNibName:@"ListenStoryHeaderCell" bundle:nil];
            [self.collectionView registerNib:nib1 forCellWithReuseIdentifier:@"ListenStoryHeaderCell"];
        }
            break;
        case UIButtonLearnAbilitys:
        {
        [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LearnAbilitysHeaderone"];
        [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LearnAbilitysHeadertwo"];
        UINib *nib1 = [UINib nibWithNibName:@"ListenStoryHeaderCell" bundle:nil];
        [self.collectionView registerNib:nib1 forCellWithReuseIdentifier:@"ListenStoryHeaderCell"];
        [self.collectionView registerClass:[LearnSkillBottomCell class] forCellWithReuseIdentifier:@"LearnSkillBottomCell"];
        UINib *nib3 = [UINib nibWithNibName:@"LearnSkillMiddleCell" bundle:nil];
        [self.collectionView registerNib:nib3 forCellWithReuseIdentifier:@"LearnSkillMiddleCell"];
        }
        case UIButtonListenStory:
        {
            [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"listenHeader"];
            UINib *nib = [UINib nibWithNibName:@"ListenStoryCell" bundle:nil];
            [self.collectionView registerNib:nib forCellWithReuseIdentifier:@"ListenStoryCell"];
            UINib *nib1 = [UINib nibWithNibName:@"ListenStoryHeaderCell" bundle:nil];
            [self.collectionView registerNib:nib1 forCellWithReuseIdentifier:@"ListenStoryHeaderCell"];
        }
        default:
            break;
    }
}
#pragma mark - collectionView的代理方法
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    if (_buttonType == UIButtonListenStory) {
        return 2;
    }else if (_buttonType == UIButtonIntelligent){
        return 2;
    }else{
        return 3;
    }

}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (_buttonType == UIButtonListenStory) {
        if (section == 0) {
            return 1;
        }else{
            return self.ListenStoryBottomArray.count;
        }
    }else if (_buttonType == UIButtonIntelligent){
        if (section == 0) {
            return 1;
        }else{
            return 3;
        }
    }else{
        if (section == 0 || section == 1) {
            return 1;
        }else{
            return self.LearSkillBottomArray.count;
        }
    }

}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (_buttonType == UIButtonListenStory) {
        if (indexPath.section == 0) {
            ListenStoryHeaderCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ListenStoryHeaderCell" forIndexPath:indexPath];
            [self  loopViewDelegate:self.headerView AndAddToFatherView:cell.contentView];
            return cell;
        }else{
            ListenStoryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ListenStoryCell" forIndexPath:indexPath];
            cell.model = self.ListenStoryBottomArray[indexPath.row];
            cell.bottomLabel.adjustsFontSizeToFitWidth = YES;
            return cell;
        }
    }else if (_buttonType == UIButtonIntelligent){
        if (indexPath.section == 0) {
            ListenStoryHeaderCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ListenStoryHeaderCell" forIndexPath:indexPath];
            [self  loopViewDelegate:self.headerView AndAddToFatherView:cell.contentView];
            return cell;
        }else{
            IntelligentCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"IntelligentCell" forIndexPath:indexPath];
            cell.BgImageView.image = [UIImage imageNamed:@"biglist"];
            return cell;
        }
    }else{
        if (indexPath.section == 0) {
            ListenStoryHeaderCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ListenStoryHeaderCell" forIndexPath:indexPath];
            [self  loopViewDelegate:self.headerView AndAddToFatherView:cell.contentView];
            return cell;
        }else if (indexPath.section == 1){
            LearnSkillMiddleCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LearnSkillMiddleCell" forIndexPath:indexPath];
            [self setupUI:cell.contentView];
            return cell;
        }else{
            static NSString *cellId = @"LearnSkillBottomCell";
            LearnSkillBottomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
            for (UIView *view in cell.contentView.subviews) {
                [view removeFromSuperview];
            }
            cell.model = self.LearSkillBottomArray[indexPath.row];
            return cell;
        }
  }

}
//设置item的偏移量
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    if (_buttonType == UIButtonListenStory) {
        if (section == 0) {
            return UIEdgeInsetsMake(0, 0, 0, 0);
        }else{
            return UIEdgeInsetsMake(0, 10, 0, 10);
        }

    }else if (_buttonType == UIButtonIntelligent){
        if (section == 0) {
            return UIEdgeInsetsMake(0, 0, 0, 0);
        }else{
            return UIEdgeInsetsMake(0, 5, 0, 5);
        }
    }else{
        return UIEdgeInsetsMake(0, 0, 0, 0);
    }
    
}
#pragma mark - 注册collectionView的头部视图

//collectionview的头部视图的设置
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

    if ([kind isEqual:UICollectionElementKindSectionHeader]) {
        if (indexPath.section == 0) {
            return nil;
        }else{
            
            if (_buttonType == UIButtonListenStory) {
                UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"listenHeader" forIndexPath:indexPath];
                view.backgroundColor = [UIColor whiteColor];
                for (UIView *subView in view.subviews) {
                    [subView removeFromSuperview];
                }
                [self addHeaderViewContent:view AndChineseTitle:@"听故事" AndEnglishTitle:@"listen to the story"];
                return view;
                
            }else if(_buttonType == UIButtonIntelligent){
                UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"intelligentHeader" forIndexPath:indexPath];
                for (UIView *subView in view.subviews) {
                    [subView removeFromSuperview];
                }
                view.backgroundColor = [UIColor whiteColor];
                [self addHeaderViewContent:view AndChineseTitle:@"达人榜" AndEnglishTitle:@"List of talent"];
                return view;
                
            }else{
                if (indexPath.section == 1) {
                    UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LearnAbilitysHeaderone" forIndexPath:indexPath];
                    for (UIView *subView in view.subviews) {
                        [subView removeFromSuperview];
                    }
                    view.backgroundColor = [UIColor whiteColor];
                    [self addHeaderViewContent:view AndChineseTitle:@"课程分类" AndEnglishTitle:@"course classification"];
                    return view;
                    
                }else{
                    UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LearnAbilitysHeadertwo" forIndexPath:indexPath];
                    for (UIView *subView in view.subviews) {
                        [subView removeFromSuperview];
                    }
                    [self addHeaderViewContent:view AndChineseTitle:@"最新课程" AndEnglishTitle:@"The latest curriculum"];
                    return view;
                }
            }
        }

    }else{
        UICollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Collectionfooter" forIndexPath:indexPath];
        view.backgroundColor = [UIColor lightGrayColor];
        return view;
    }
}
//设置头部视图的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return CGSizeMake(0, 0);
    }
    return CGSizeMake(WIDTH, scaleTopAndBottom(50));
}
//设置尾视图
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return CGSizeMake(WIDTH, scaleTopAndBottom(5));
}
//设置item的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (_buttonType == UIButtonListenStory) {
        if (indexPath.section == 0) {
            return CGSizeMake(WIDTH, scaleTopAndBottom(175));
        }else{
            return CGSizeMake(scaleLeftAndRight(172.5), scaleTopAndBottom(130));
        }
    }else if (_buttonType == UIButtonIntelligent){
        if (indexPath.section == 0) {
            return CGSizeMake(WIDTH, scaleTopAndBottom(175));
        }else{
            return CGSizeMake(WIDTH - scaleLeftAndRight(40), scaleTopAndBottom(175));
        }
    }else{
        if (indexPath.section == 0) {
            return CGSizeMake(WIDTH, scaleTopAndBottom(175));
        }else if (indexPath.section == 1){
            return CGSizeMake(WIDTH, scaleTopAndBottom(150));
        }else{
            return CGSizeMake(WIDTH, scaleTopAndBottom(280));
        }
    }
}
//设置collectionview中的item的行间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    if (_buttonType == UIButtonListenStory) {
        if (section == 0) {
            return 0;
        }
        return scaleTopAndBottom(1);
    }else if (_buttonType == UIButtonIntelligent){
        if (section == 0) {
            return 0;
        }
        return scaleTopAndBottom(20);
    }else{
        if (section == 0) {
            return 0;
        }
        return scaleTopAndBottom(1);
    }

}
//设置collectionview中的item的列间距
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    if (_buttonType == UIButtonListenStory) {
        if (section == 0) {
            return 0;
        }
        return scaleLeftAndRight(5);
    }else if (_buttonType == UIButtonIntelligent){
        return 0;
    }else{
        if (section == 0) {
            return 0;
        }
        return scaleTopAndBottom(1);
    }
    
}
#pragma mark --collectionView的选中状态
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (_buttonType == UIButtonIntelligent) {
        if (indexPath.section == 1) {
            if (indexPath.row == 0) {
                CelebrityListViewController *listVc = [[CelebrityListViewController alloc] init];
                listVc.type = @"0";
                [self.navigationController pushViewController:listVc animated:YES];
            }else if (indexPath.row == 1){
                BigAndSmallPersonViewController *vc = [[BigAndSmallPersonViewController alloc] init];
                vc.title = @"大咖达人";
                 vc.fileType = @"1";
                [self.navigationController pushViewController:vc animated:YES];
            }else{
//                 BigAndSmallPersonViewController *vc = [[BigAndSmallPersonViewController alloc] init];
//                vc.title = @"榜样小咖";
//                vc.fileType = @"2";
//                [self.navigationController pushViewController:vc animated:YES];
            }

                
            }
    }else if (_buttonType == UIButtonLearnAbilitys){
        if (indexPath.section == 0) {
            
        }else if (indexPath.section == 1){

            
        }else{
            IntorduceAndCatalogViewController  *detailVc = [[IntorduceAndCatalogViewController alloc] init];
            OpusListModel *model = self.LearSkillBottomArray[indexPath.row];
            detailVc.inforId = model.opusId;
            detailVc.type = @"3";
            [self.navigationController pushViewController:detailVc animated:YES];
        }
    }else{
        if (indexPath.section == 1) {
           CelebrityListViewController *vc = [[CelebrityListViewController alloc] init];
            OpusListModel *model = self.ListenStoryBottomArray[indexPath.row];
            vc.opusId = model.opusId;
            vc.type = @"4";
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
}
#pragma mark -头部视图
-(void)addHeaderViewContent:(UIView *)titleView AndChineseTitle:(NSString *)chineTile AndEnglishTitle:(NSString *)engTitle{
    UILabel *label = [[UILabel alloc] init];
    label.text = chineTile;
    label.adjustsFontSizeToFitWidth = YES;
    label.textAlignment = NSTextAlignmentCenter;
    [titleView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(titleView.mas_centerX);
        make.centerY.equalTo(titleView.mas_centerY).offset(scaleTopAndBottom(-5));
        make.width.mas_equalTo(scaleLeftAndRight(80));
        make.height.mas_equalTo(scaleTopAndBottom(20));
    }];
    UIView *view1 = [[UIView alloc] init];
    view1.backgroundColor = [UIColor lightGrayColor];
    [titleView addSubview:view1];
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleView.mas_left).offset(scaleLeftAndRight(80));
        make.centerY.equalTo(titleView.mas_centerY).offset(scaleTopAndBottom(-5));
        make.height.mas_equalTo(scaleTopAndBottom(1));
        make.right.equalTo(label.mas_left).offset(scaleLeftAndRight(-5));
    }];
    UIView *view2 = [[UIView alloc] init];
    view2.backgroundColor = [UIColor lightGrayColor];
    [titleView addSubview:view2];
    [view2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(titleView.mas_right).offset(scaleLeftAndRight(-80));
        make.centerY.equalTo(titleView.mas_centerY).offset(scaleTopAndBottom(-5));
        make.height.mas_equalTo(scaleTopAndBottom(1));
        make.left.equalTo(label.mas_right).offset(scaleLeftAndRight(5));
    }];
    UILabel *bottomLabel = [[UILabel alloc] init];
    bottomLabel.text = engTitle;
    bottomLabel.adjustsFontSizeToFitWidth = YES;
    bottomLabel.textAlignment = NSTextAlignmentCenter;
    [titleView addSubview:bottomLabel];
    [bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(label.mas_bottom).offset(0);
        make.width.mas_equalTo(WIDTH);
        make.height.mas_equalTo(scaleTopAndBottom(20));
        make.left.equalTo(titleView.mas_left);
    }];
    
}
#pragma mark -创建学本领中间的图片立体手动轮播
- (void)setupUI:(UIView *)view{
    //设置高度 WIDTH * 9 / 16
    NewPagedFlowView *pageFlowView = [[NewPagedFlowView alloc] initWithFrame:CGRectMake(0, scaleTopAndBottom(0), WIDTH, scaleTopAndBottom(150))];
    pageFlowView.delegate = self;
    pageFlowView.dataSource = self;
    pageFlowView.minimumPageAlpha = 0.1;
    pageFlowView.isCarousel = NO;
    pageFlowView.orientation = NewPagedFlowViewOrientationHorizontal;
    pageFlowView.isOpenAutoScroll = YES;
    
    //初始化pageControl
    /****************************
     使用导航控制器(UINavigationController)
     如果控制器中不存在UIScrollView或者继承自UIScrollView的UI控件
     请使用UIScrollView作为NewPagedFlowView的容器View,才会显示正常,如下
     *****************************/
    UIScrollView *bottomScrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    [bottomScrollView addSubview:pageFlowView];
    [pageFlowView reloadData];
    [view addSubview:bottomScrollView];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
#pragma mark NewPagedFlowView Delegate
- (CGSize)sizeForPageInFlowView:(NewPagedFlowView *)flowView {
    return CGSizeMake(WIDTH -scaleLeftAndRight(60), scaleTopAndBottom(150));
}
- (void)didSelectCell:(UIView *)subView withSubViewIndex:(NSInteger)subIndex {
    OpusListModel *model = self.imageArra[subIndex];
    CelebrityListViewController *vc = [[CelebrityListViewController alloc] init];
    vc.type = @"3";
    vc.opusId = model.opusId;
    [self.navigationController pushViewController:vc animated:YES];
    NSLog(@"点击了第%ld张图和id%@",(long)subIndex + 1,model.opusId);
}

#pragma mark NewPagedFlowView Datasource
- (NSInteger)numberOfPagesInFlowView:(NewPagedFlowView *)flowView {
    return self.imageArra.count;
}
- (UIView *)flowView:(NewPagedFlowView *)flowView cellForPageAtIndex:(NSInteger)index{
    PGIndexBannerSubiew *bannerView = (PGIndexBannerSubiew *)[flowView dequeueReusableCell];
    if (!bannerView) {
        bannerView = [[PGIndexBannerSubiew alloc] initWithFrame:CGRectMake(0, 0,WIDTH - scaleLeftAndRight(60), scaleTopAndBottom(150))];
        bannerView.tag = index;
        bannerView.layer.cornerRadius = 4;
        bannerView.layer.masksToBounds = YES;
    }
    //在这里下载网络图片
    OpusListModel *model = self.imageArra[index];
    [bannerView.mainImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",PictureIp,model.imagepath]] placeholderImage:[UIImage imageNamed:@""]];
    bannerView.titleLabel.text = model.name;
    return bannerView;
}

- (void)didScrollToPage:(NSInteger)pageNumber inFlowView:(NewPagedFlowView *)flowView {
    NSLog(@"ViewController 滚动到了第%ld页",pageNumber);
}
#pragma mark -数组懒加载
-(NSMutableArray *)imageArra{
    if (_imageArra == nil) {
        _imageArra = [[NSMutableArray alloc] init];

    }
    return _imageArra;
}
-(NSMutableArray *)LearSkillBottomArray{
    if (_LearSkillBottomArray == nil) {
        _LearSkillBottomArray = [[NSMutableArray alloc] init];
    }
    return _LearSkillBottomArray;
}
#pragma mark -头部的按钮点击事件
-(void)ButtonDialAndResponder:(UIButton *)btn{
    if (btn.tag == 1000) {
        _buttonType = UIButtonIntelligent;
        [self createCollectionUI:_headerView];
    }
    if (btn.tag == 1001) {
        _buttonType = UIButtonLearnAbilitys;
        [self.imageArra removeAllObjects];
        [self createCollectionUI:_headerView];
        [self getMiddlePictures];
        [self addRefreshWithTpye:@"3" AndArray:self.LearSkillBottomArray];
        [self.collectionView.mj_header beginRefreshing];
    }
    if (btn.tag == 1002) {
        _buttonType = UIButtonListenStory;
        [self createCollectionUI:_headerView];
        [self addRefreshWithTpye:@"4" AndArray:self.ListenStoryBottomArray];
        [self.collectionView.mj_header beginRefreshing];
    }
}
//获取数据
-(void)getHomePageAllData:(NSString *)fileType AndAddToArray:(NSMutableArray *)dataArray AndshowCount:(NSInteger) showCount AndCurrentPage:(NSInteger) currentPage{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSDictionary *param = @{@"type":fileType,@"showCount":[NSString stringWithFormat:@"%ld",(long)showCount],@"currentPage":[NSString stringWithFormat:@"%ld",(long)currentPage]};
    NSString *url = ServerIp@"/opusApp/list";
    [manager POST:url parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSArray *arra = jsonDict[@"list"];
        NSMutableArray *array11 = [[NSMutableArray alloc] init];
        for (NSDictionary *dict in arra) {
          OpusListModel *model = [OpusListModel modelWithDictionary:dict];
            [array11 addObject:model];
        }
        if (_currentPage == 1) {
            [dataArray removeAllObjects];
        }
        [dataArray addObjectsFromArray:array11];
        [self.collectionView reloadData];
        [self.collectionView.mj_header endRefreshing];
        [self.collectionView.mj_footer endRefreshing];

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"错误信息%@",error);
    }];
}
-(NSMutableArray *)ListenStoryBottomArray{
    if (_ListenStoryBottomArray == nil) {
        _ListenStoryBottomArray = [[NSMutableArray alloc] init];
    }
    return _ListenStoryBottomArray;
}
#pragma mark -MJRefresh
-(void)addRefreshWithTpye:(NSString *)type AndArray:(NSMutableArray *)arra{
//    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refreshNewData)];
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
         _showCount = 10;
        _currentPage = 1;
        [self getHomePageAllData:type AndAddToArray:arra AndshowCount:_showCount AndCurrentPage:_currentPage];
    }];
    self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _showCount = 10;
        _currentPage ++;
        [self getHomePageAllData:type AndAddToArray:arra AndshowCount:_showCount AndCurrentPage:_currentPage];
    }];

}

//获取学本领中间的图片信息
-(void)getMiddlePictures{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
//    NSDictionary *param = @{@"type":fileType};
    NSString *url = ServerIp@"/opusCategoryApp/list";
    [manager GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
                NSArray *arra = jsonDict[@"list"];
                for (NSDictionary *dict in arra) {
                    OpusListModel *model = [OpusListModel modelWithDictionary:dict];
                [self.imageArra addObject:model];
                }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];

}

@end
